package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.User;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    //分页查询服务(带条件)
    public PageInfo<User> queryByPage(int pageNum, User user) {//当前页码
        int pageSize = 4;//每页记录数量
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象pageInfo,构造器里需求查询结果，进行分页查询
        List<User> users;
        if (user == null) {//无条件查询
            users = userMapper.selectAll();
        } else {//有条件查询
            users = userMapper.selectByConditions(user);
        }
        PageInfo pageInfo = new PageInfo(users);//翻页信息对象
        return pageInfo;
    }

    //删除用户服务
    public boolean removeuser(String userid) {
        int rs = userMapper.deleteById(userid);
        if (rs == 1) {//删除了一条数据
            return true;
        } else {
            return false;
        }
    }

    //添加用户服务
    public boolean adduser(User user){
        int rs = userMapper.insert(user);
        if (rs==1) {//插入成功
            return true;
        }else{//插入失败
            return false;
        }
    }

    //修改服务
    public boolean modifyuser(User user){
        int rs = userMapper.update(user);
        if (rs==1) {//修改成功
            return true;
        }else{//修改失败
            return false;
        }
    }

    //查询个人信息
    public User queryuser(String userid){
        return userMapper.selectById(userid);
    }

}
